var map = null; //地图对象
var startPoint=null;
var endPoint=null;
var polygon=null;
/*var otherPolygon=[];
var otherPolygonLabel=[];*/
var start=[];
var end=[];
var driving=null;
var ways=[];
var ctx = window.daijia.ctx;

var mapLoadFinish = false;


function initialize(center){
	
	var myGeo = new BMap.Geocoder();
	
	myGeo.getPoint(center, function(point){
		if (point) {
			map = new BMap.Map("allmap");
			map.centerAndZoom(point, 13);
		    map.enableScrollWheelZoom();
		    ///三种驾车策略：最少时间，最短距离，避开高速    BMAP_DRIVING_POLICY_LEAST_TIME,BMAP_DRIVING_POLICY_LEAST_DISTANCE,BMAP_DRIVING_POLICY_AVOID_HIGHWAYS
		    driving = new BMap.DrivingRoute(map, {renderOptions:{map: map},policy : BMAP_DRIVING_POLICY_LEAST_DISTANCE});
		    //右键菜单
		    var contextMenu = new BMap.ContextMenu();
			var txtMenuItem = [{
			   text:'设为起始点',
			   callback:function(p){
				   if(startPoint){
					   startPoint.remove(true);
				   }
				   start[0]=p.lng;
				   start[1]=p.lat;
				   startPoint = new BMap.Marker(p);
				   map.addOverlay(startPoint);
				   
				   if(startPoint && endPoint){
					   driving.search(startPoint.getPosition(),endPoint.getPosition(),{waypoints:ways});
				   }
			   }
			},{
				   text:'设为途径地',
				   callback:function(p){
					  
					   ways[ways.length]=p;
					   if(startPoint && endPoint){
						   driving.search(startPoint.getPosition(),endPoint.getPosition(),{waypoints:ways});
					   }
				   }
				},{
			   text:'设为结束点',
			   callback:function(p){
				   if(endPoint){
					   endPoint.remove(true);
				   }
				   endPoint = new BMap.Marker(p);
				 /*  var label = new BMap.Label("结束点",{offset:new BMap.Size(20,-10)});
				   endPoint.setLabel(label);*/
				   map.addOverlay(endPoint);
				   
				   if(startPoint && endPoint){
					   driving.search(startPoint.getPosition(),endPoint.getPosition(),{waypoints:ways});
				   }
			   }
			},{
			   text:'清除',
			   callback:function(p){
				   
				   map.clearOverlays();
				   start=[];
				   end=[];
				   ways=[];
				   startPoint=null;
				   endPoint=null;
				  /* if(startPoint){
					   startPoint.remove(true);
					   startPoint=null;
				   }
				   if(endPoint){
					   endPoint.remove(true);
					   endPoint=null;
				   }
				   if(polygon){
					   polygon.remove(true);
					   polygon=null;
				   }
				   $("#startPointLng").val("");
				   $("#startPointLat").val("");
				   $("#endPointLng").val("");
				   $("#endPointLat").val("");*/
			   }
			}];
			
			for(var i=0; i < txtMenuItem.length; i++){
				contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
				if(i==1 || i==3) {
					contextMenu.addSeparator();
				}
			}
			map.addContextMenu(contextMenu); 
			

		    //地图加载完毕
		    map.addEventListener('tilesloaded',function(){
		    	
		    	if(!mapLoadFinish){
		    		
		    		mapLoadFinish = true;
		    		
		    		if($("#startPointLng").val()!="" && $("#startPointLat").val()!="" && $("#endPointLng").val()!="" && $("#endPointLat").val()!=""){
		    			startPoint = new BMap.Marker(new BMap.Point($("#startPointLng").val(), $("#startPointLat").val()));
			    		var label = new BMap.Label("起始点",{offset:new BMap.Size(20,-10)});
			    		startPoint.setLabel(label);
			    		map.addOverlay(startPoint);
					   
			    		endPoint = new BMap.Marker(new BMap.Point($("#endPointLng").val(), $("#endPointLat").val()));
			    		var label = new BMap.Label("结束点",{offset:new BMap.Size(20,-10)});
			    		endPoint.setLabel(label);
			    		map.addOverlay(endPoint);
			    		
			    		drawArea(startPoint.getPosition(),endPoint.getPosition());
		    		}
		    		
		    		
		    		if(window.daijia.areaId){
		    			$.get(ctx+'/workcararea/getByCity',{'city':$("#city").val()},function(result){
		    				if(result.success){
		    					$(result.data).each(function(index,area){
	    							if(window.daijia.areaId==area.id){
	    								
	    							}
	    							else{
	    								createOtherArea(area);
	    							}
		    					});
		    				}
		    			});
		    		}
		    		else{
		    			$("#city").val(window.daijia.center);
		    			$("#city").change();
		    		}
		    		
		    	}
		    	
		   });  
		   
		}
		else{
			$.scojs_message("地区"+center+"位置不准确，请重新编辑地区", $.scojs_message.TYPE_ERROR);
		}
	});
}

/*function drawArea(positionA,positionB){
	
	if(polygon){
		polygon.remove(true);
	}
	
	polygon = new BMap.Polygon([
	    new BMap.Point(positionA.lng,positionA.lat),
	    new BMap.Point(positionB.lng,positionA.lat),
	    new BMap.Point(positionB.lng,positionB.lat),
	    new BMap.Point(positionA.lng,positionB.lat)
	], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});
	
	map.addOverlay(polygon);
	
	$("#startPointLng").val(positionA.lng);
	$("#startPointLat").val(positionA.lat);
	$("#endPointLng").val(positionB.lng);
	$("#endPointLat").val(positionB.lat);
	
}

function createOtherArea(area){
	
	var otherpolygon = new BMap.Polygon([
	    new BMap.Point(area.startPointLng,area.startPointLat),
	    new BMap.Point(area.endPointLng,area.startPointLat),
	    new BMap.Point(area.endPointLng,area.endPointLat),
	    new BMap.Point(area.startPointLng,area.endPointLat)
	], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});
	map.addOverlay(otherpolygon);
	
	otherPolygon.push(otherpolygon);
	
	var point = new BMap.Point((area.startPointLng+area.endPointLng)/2,(area.startPointLat+area.endPointLat)/2);
	var opts = {
	  position : point,    // 指定文本标注所在的地理位置
	  offset   : new BMap.Size(-10, -10)    //设置文本偏移量
	}
	var label = new BMap.Label(area.name, opts);  // 创建文本标注对象
	label.setStyle({
		 color : "red",
		 fontSize : "12px",
		 height : "20px",
		 lineHeight : "20px",
		 fontFamily:"微软雅黑"
	});
	map.addOverlay(label);
	
	otherPolygonLabel.push(label);
}*/

$(document).ready(function(){
	
	//初始化地图
	initialize(window.daijia.center);
	$("#city").change(function(){
		map.centerAndZoom($(this).val(), 11);
		
	});
	$("#submit_form").submit(function(){
		if($("#startPointLng").val()=="" || $("#startPointLat").val()=="" || $("#endPointLng").val()=="" || $("#endPointLat").val()==""){
			$.scojs_message("请在地图中选择区域", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#startPointLng").val()>$("#endPointLng").val()){
			$.scojs_message("结束点必须在起始点右边", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#startPointLat").val()<$("#endPointLat").val()){
			$.scojs_message("结束点必须在起始点下边", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		return true;
	});
/*	
	$("#city").change(function(){
		if($(this).val()!=""){
			$.get(ctx+'/workcararea/getByCity',{'city':$(this).val()},function(result){
				if(result.success){
					
					if(otherPolygon && otherPolygon.length>0){
						$(otherPolygon).each(function(i,e){
							e.remove(true);
						});
					}
					if(otherPolygonLabel && otherPolygonLabel.length>0){
						$(otherPolygonLabel).each(function(i,e){
							e.remove(true);
						});
					}
					
					$(result.data).each(function(index,area){
						if(window.daijia.areaId){
							if(window.daijia.areaId==area.id){
								
							}
							else{
								createOtherArea(area);
							}
						}
						else{
							createOtherArea(area);
						}
					});
				}
			});
		}
		else{
			if(otherPolygon && otherPolygon.length>0){
				$(otherPolygon).each(function(i,e){
					e.remove(true);
				});
			}
			if(otherPolygonLabel && otherPolygonLabel.length>0){
				$(otherPolygonLabel).each(function(i,e){
					e.remove(true);
				});
			}
		}
	});*/
	
	

});

